package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.LightBillModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
/**
 * 电费业务
 * @author zhaokf
 * @date 2013-04-22
 */
public class LightBillService extends BaseService
{
	/**
	 * 分页列出电费
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<LightBillModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("prc_jfq") != null){
				where = " AND prc_jfq=" + search.get("prc_jfq");
			}
			if (search.get("prc_code") != null){
				where = " AND prc_code LIKE '%" + search.get("prc_code") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<LightBillModel> bills = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM SUP_PRC " + where;

		try{
			bills = LightBillModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return bills;
	}



	/**
	 * 增加电费信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		String SQL = "insert into SUP_PRC(prc_jfq,prc_code,prc_name) values(?,?,?)";
		int isupadded = Db.update(SQL, params.get("prc_jfq"),params.get("prc_code"),params.get("prc_name"));
		if(isupadded==1){
			return true;
		}
		return setError("你访问的电费信息已存在！");
	}

	/**
	 * 修改电费信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		String SQL = "update SUP_PRC set prc_jfq=?,prc_code=?,prc_name=?";
		int isupadded = Db.update(SQL, params.get("prc_jfq"),params.get("prc_code"),params.get("prc_name"));
		if(isupadded==1){
			return true;
		}
		return setError("您访问的电费信息不存在！");
	}

	/**
	 * 删除电费信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("prc_code")==null?"":params.get("prc_code").toString();
		String sql = "select * from SUP_PRC where prc_code=?";
		LightBillModel bm = LightBillModel.dao.findFirst(sql, no);
		if (bm != null)
		{
			bm.delete();
			return true;
		}
		return setError("您访问的电费信息不存在！");
	}



	public static LightBillModel get(Map<String, String> params)
	{
		String no = params.get("prc_code")==null?"":params.get("prc_code").toString();
		LightBillModel lm = LightBillModel.dao.findFirst("SELECT * FROM SUP_PRC WHERE prc_code=?", no);
		return lm;
	}
}

